<template>
	<jk-dialog
		v-model="showDialog"
		title="选择-到货单"
		width="1100px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4 flex-between-center">
			<!--			<div class="flex-end-center">-->
			<!--				<jk-search-input v-model="searchParams.code" class="search-bar-item-size3 item-gap" placeholder="编号" @on-enter="onSearchBtn"></jk-search-input>-->
			<!--				<jk-search-input v-model="searchParams.primaryBatchCode" class="search-bar-item-size2 item-gap" placeholder="原批" @clear="onSearchBtn" @on-enter="onSearchBtn"></jk-search-input>-->
			<!--				<jk-search-button @click="onSearchBtn"></jk-search-button>-->
			<!--			</div>-->
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="tableHeight - 80"
			:checkbox-config="{checkMethod: checkboxMethod}"
			@checkbox-change="onCheckBoxChange"
		>
			<vxe-column type="checkbox" width="60" align="center"></vxe-column>
			<vxe-table-column field="code" title="编号" :width="144" />
			<vxe-table-column field="carCode" title="车号" :min-width="80" />
			<vxe-table-column field="belongDate" title="到货日期" :min-width="100"> </vxe-table-column>
			<vxe-table-column field="primaryBatchCode" title="原批" :min-width="140"> </vxe-table-column>
			<vxe-table-column field="num" title="件数" :width="70" />
			<vxe-table-column field="payment" title="付款" align="right" :width="90" />
			<vxe-table-column field="terminal" align="right" title="到站" :width="80" />
			<vxe-table-column field="weighNum" align="right" title="已分配数量" :width="80" />
			<vxe-table-column field="residueNum" align="right" title="未分配数量" :width="80" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../../../mixins/cal-table-height';
    import { pageConfig, tipsBar } from '../../../../../../utils/jk/jk-tool';
    import { productionArrivedGoodsGetByProductCode } from '../../../../../../api/production/arrivedGoodsOrder';

    export default {
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            productName: {
                type: String,
                default: ''
            },
            primaryBatchCode: {
                type: String,
                default: ''
            },
            dialogStateDisabled: {
                type: String,
                default: ''
            },
            checkKeyName: {
                type: String,
                default: 'arrivedGoodsItemCode'
            },
            defaultSelectRows: {
                type: Array,
                default() {
                    return [];
                }
            },
            checkDataArr: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data() {
            return {
                totalCount: 0,
                searchParams: {
                    pageNo: 1,
                    pageSize: pageConfig.pageSize,
                    productCode: null,
                    name: null,
                    primaryBatchCode: '',
                    code: ''
                },
                searchBarProcessId: null,
                tableData: [],
                showDialog: false,
                tableLoading: false,
                checkData: []
            };
        },
        methods: {
            validatorCheckData() {
                const arr = [
                    ...JSON.parse(JSON.stringify(this.defaultSelectRows)),
                    ...JSON.parse(JSON.stringify(this.checkData))
                ];
                let ids = arr.map(x => x.productName);
                ids = Array.from(new Set(ids));
                return ids;
            },
            onCheckBoxChange({ records }) {
                this.checkData = records.filter(x => !x._disabled).map(x => {
                    return x;
                });
                if (this.validatorCheckData().length > 1) {
                }
            },
            checkboxMethod(e) {
                return !e.row._disabled;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (this.checkData.length) {
                    if (this.validatorCheckData().length !== 0) {
                        const arr = this.checkData.filter(x => !x._disabled);
                        this.$emit('on-confirm', arr);
                        this.$emit('on-visible-change', false);
                    }
                } else {
                    tipsBar(this, 'checkbox');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                this.checkData = [];
                return productionArrivedGoodsGetByProductCode(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count || 0;
                        this.tableData = res.data.res || [];
                        this.setCheckState(res.data.res || []);
                    }
                });
            },
            setCheckState(arr) {
                arr.forEach((x) => {
                    x._disabled = false;
                    this.checkDataArr.forEach(k => {
                        if (x.code === k[this.checkKeyName]) {
                            x._disabled = true;
                        }
                    });
                });
                this.tableData = arr;
                this.tableData.forEach((x, index) => {
                    if (x._disabled) {
                        this.$refs.jkTable.$refs.xTable.setCheckboxRow([this.tableData[index], this.tableData[index]], true);
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                if (!this.searchParams.productCode) {
                    this.$set(this.searchParams, 'productCode', this.dialogStateDisabled);
                }
                if (!this.searchParams.primaryBatchCode) {
                    this.$set(this.searchParams, 'primaryBatchCode', this.primaryBatchCode);
                }
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
